/*
 * Copyright 2009, Qys Creations 
 * 
 *  This file is part of Chameleon DB.
 *
 *  Chameleon DB is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Chameleon DB is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Chameleon DB.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * */



import java.io.*;
import java.net.*;
import java.util.*;

public class db_connect {

	/*
	 * This is the main object.
	 * Clients connect by TCP request with a HTTP-like protocol
	 * A number of threads consumes the connections from a thread safe queue 
	 * when a client connects the protocol and various information is negotiated
	 * then the client is free to send SQL queries in plain text
	 * when a query is received the thread tries to parse the query.
	 * on success a subclass of db_interface_parse_result object is returned, with a run_query method 
	 * that will invoke the query.
	 * 
	 * The actual processing of the query happens in the db_database object.
	 *  
	 * */
	
	private db_server oServer;
	private db_query_queue oQueue;
	
	public static void main(String[] args) {
		// lets get out of this static context
		new db_connect();
	}
	
	db_connect (){		
		// Create a request queue, and a global server object
		// Each thread uses the server object to access the databases
	
		oQueue = new db_query_queue();
		oServer = new db_server(oQueue);

		// Load databases from directory specified in db_util
		
		File oDirectory  = new File(db_util.DB_DATABASE_FOLDER);
	    FilenameFilter oFilter = new FilenameFilter() {
	        public boolean accept(File dir, String name) {
	            return name.endsWith(db_util.DB_DATABASE_FILENAME_EXT);
	        }
	    };
	   
	    // Some debug info
		String[] sDBPaths = oDirectory.list(oFilter);
		 
		for (int i = 0; i < sDBPaths.length; i++) {
			// Add the database to the gloabl server object
			System.out.println(db_util.DB_DATABASE_FOLDER + sDBPaths[i]);
			oServer.oDatabases.add(new db_database(db_util.DB_DATABASE_FOLDER + sDBPaths[i]));
			System.out.println("Loaded database: " + oServer.oDatabases.get(i).sName);
		}

		// Boot up threads
		
		for (int i = 0; i < db_util.DB_NUMBER_OF_THREADS; i++) {
			 new db_thread(oServer).start();
		}
		
		// More debug info
		
		System.out.println("QDB now accepts TCP requests on port: " + db_util.DB_TCP_PORT_NUMBER);
		
		try {
			// Start listening for requests
			
			ServerSocket oSocket = new ServerSocket(db_util.DB_TCP_PORT_NUMBER);
			while (true) {
				oQueue.add_query(oSocket.accept());
			}
		} catch (Exception e) {e.printStackTrace();};
		
	}
}
